#include <iostream>
#include <ostream>
struct ListNode {
    int val;
    ListNode *next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode *next) : val(x), next(next) {}
 };

int curr = 0;

// 删除第 n 个节点，并返回删除节点后的链表的头节点
ListNode* removeNth(ListNode* head, int n) {
    if (head == nullptr) return nullptr;
    curr++;
    if (curr == n) {
        return head->next;
    }
    ListNode* ret = removeNth(head.next, n);
    head->next = ret;
    return head;
}

// 删除倒数第 n 个节点，并返回删除节点后的链表的头节点
ListNode* removeNthFromEnd(ListNode* head, int n) {
    if (head == nullptr) return nullptr;

    ListNode* ret = removeNthFromEnd(head.next, n);
    head->next = ret;

    curr++;
    if (curr == n) {
        return head->next;
    }
    return head;
}